Components.utils.reportError("Fireice.js is without formatting errors");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

const Cc = Components.classes;
const Ci = Components.interfaces;

function Fireice() {
	this.recording=false;	
	/* Add initialization code here */
}

Fireice.prototype = {
	classDescription: "This xpcom Component will provide an API for the extension",
	classID: Components.ID("{9200f18a-6d31-4eb8-81df-2f0b6653f691}"),
	contractID: "@abhinav.chittora/Fireice;1",
	QueryInterface: XPCOMUtils.generateQI(
		[Ci.nsIFireice,
		Ci.nsISecurityCheckedComponent,
		Ci.nsISupportsWeakReference,
		Ci.nsIClassInfo]),
	_xpcom_factory : FireiceFactory,
	_xpcom_categories : [{
		category: "JavaScript global constructor",
		entry: "Fireice"
	}],
	implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
	flags: Ci.nsIClassInfo.DOM_OBJECT,
	getInterfaces: function getInterfaces(aCount) 
	{
		var interfaces = [Ci.nsIFireice,
	                      Ci.nsISecurityCheckedComponent,
	                      Ci.nsISupportsWeakReference,
	                      Ci.nsIClassInfo];
		aCount.value = interfaces.length;
		return interfaces;
  	},
	getHelperForLanguage: function getHelperForLanguage(aLanguage) 
	{
		return null;
	},

	//nsISecurityCheckedComponent
	canCallMethod: function canCallMethod(iid, methodName) 
	{
		Components.utils.reportError(methodName);
		return "AllAccess";
	},

	canCreateWrapper: function canCreateWrapper(iid) 
	{
		return "AllAccess";
	},

	canGetProperty: function canGetProperty(iid, propertyName) 
	{
		Components.utils.reportError(propertyName);
		return "AllAccess";
  	},

  	canSetProperty: function canSetProperty(iid, propertyName) 
	{
    		Components.utils.reportError(propertyName);
		return "NoAccess";
  	},	

	/* Add your interface methods here */
	
	//function to record the 
	record: function(str) 
	{ 
		if(!this.recording) {
		dump(this.recording);
		var firefoggId = "fireice@abhinav.chittora";
		var em = Components.classes["@mozilla.org/extensions/manager;1"]
			.getService(Components.interfaces.nsIExtensionManager);
		var xulRuntime = Components.classes["@mozilla.org/xre/app-info;1"]
			.getService(Components.interfaces.nsIXULRuntime);
		var exec_name = "bin";
		var f = em.getInstallLocation(firefoggId).getItemFile(firefoggId, exec_name);
		var cmd = f;
		cmd.append("recorder");
		//getting the installation directory
		var opt=JSON.parse(str);
		var options =new Array();
		options[0]=opt.server;
		options[1]=opt.port;
		options[2]=opt.password;
		options[3]=opt.mount;
		dump(str);

		this.recording=true;
		//creating a output console		
		this.outputConsole = Components.classes["@mozilla.org/process/pipe-console;1"]
			.createInstance(Components.interfaces.nsIPipeConsole);
		this.observer=null;
		//adding a output console
		this.outputConsole.open(10000, 0, true);
		//creating an object of IPC component
		this.ipc = Components.classes["@mozilla.org/process/ipc-service;1"]
                   	.getService().QueryInterface(Components.interfaces.nsIIPCService);
		//Calling runAsync function to run recorder
 		this.req = this.ipc.runAsync(cmd, options, options.length,"", "", 0, [], 0, this.outputConsole, null, this.observer);
		}
		else {
			dump("Error in start recording");
		}
		dump(this.recording);
	},
	//function to stop recording 
	stop:function () {
		
		try {
			dump(this.recording);
			if(this.recording)
			{
						
				this.req.close(true);
				this.recording=false;	
				dump("Recording Stoped");
			
			}
			else
			{
				dump("Error in Stop recording");
			}
		}
		catch(e)
		{
			dump("Getting Some Error :-/" +e);
			dump(this.recording);
		}
		dump(this.recording);
	},
	vu:function()
	{
		//dump(this.recording);
		if(this.recording)
		{
			if(!this.outputConsole.hasNewData || !this.outputConsole.hasNewData()) 
			{
				try 
				{
					this.vustatus = this.outputConsole.getNewData();
      				}
				catch(e) 
				{
					this.vustatus = '';
      				}
    			} 
			else 
			{
      				this.vustatus = this.outputConsole.getNewData();
    			}
		
    			this.vustatus = this.vustatus.replace(/^\s+|\s+$/g,"").split('\n'); // strip and split at newlines
    			
    			this.vustatus='{'+this.vustatus[this.vustatus.length-1]+'}';
			dump(this.vustatus);		
			return (this.vustatus);
		}
		
		
	},
	status:function()
	{
		var timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
		
	}
	
	
}
function NSGetModule(compMgr, fileSpec) {
  return XPCOMUtils.generateModule([Fireice]);
}


var FireiceFactory =
{
  createInstance: function (outer, iid)
  {
    if (outer != null)
      throw NS_ERROR_NO_AGGREGATION;

    return (new Fireice()).QueryInterface(iid);
  }
};

